package com.hnkywl.sts.service.flow;

import com.hnkywl.sts.dao.flow.FlowDao;
import com.hnkywl.sts.entity.flow.FlowTask;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.action.flow.assign.RegectTaskSingleBackWayCmd;
import net.ruixin.orm.hibernate.Page;
import org.apache.commons.lang3.StringUtils;
import org.jbpm.api.*;
import org.jbpm.api.task.Task;
import org.jbpm.pvm.internal.model.ExecutionImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-4-7
 * Time: 上午11:40
 * To change this template use File | Settings | File Templates.
 */
@Service
public class FlowService {
    private FlowDao flowDao;

    private RepositoryService repositoryService;

    private ExecutionService executionService;

    private ProcessEngine processEngine;

    private TaskService taskService;

    public Map<String, Object> findLcspForScreen() {
        return flowDao.findLcspForScreen();
    }

    public List<Map> findOwnerTask(Long ygId) {
        return this.flowDao.findOwnerTask(ygId);
    }

    public List findHistory(String executionId) {
        return this.flowDao.findHistory(executionId);
    }

    public List<Yg> findMsgUser(String executionId) {
        return this.flowDao.findMsgUser(executionId);
    }

    public void delPorcessInstance(String processInstanceId) {
        if (StringUtils.isNotEmpty(processInstanceId))
            this.executionService.deleteProcessInstanceCascade(processInstanceId);
    }

    public String regTask(String processInstanceId, String name, String contain) {
        Map<String, Object> vars = new HashMap<String, Object>();
        vars.put("owner", YgService.getCurrentYg().getId());
        ProcessInstance pi;
        if (StringUtils.isEmpty(processInstanceId)) {
            ProcessDefinition pd = repositoryService.createProcessDefinitionQuery().processDefinitionName(name).page(0, 1).orderDesc(ProcessDefinitionQuery.PROPERTY_VERSION).uniqueResult();
            pi = this.executionService.startProcessInstanceByKey(pd.getKey(), vars);
            processInstanceId = pi.getId();
        } else {
            pi = this.executionService.createProcessInstanceQuery().processInstanceId(processInstanceId).uniqueResult();
        }
        Task task = this.taskService.createTaskQuery().activityName(((ExecutionImpl) pi).getActivityName()).executionId(((ExecutionImpl) pi).getExecution().getId()).uniqueResult();
        vars.put("opera", YgService.getCurrentYgGs() + "--" + YgService.getCurrentYgName());
        vars.put("result", "申请");
        vars.put("time", new Date());
        this.executionService.createVariables(task.getExecutionId(), vars, true);

        vars.put("status", "申请");
        vars.put("contain", contain);
        this.taskService.completeTask(task.getId(), vars);
        return processInstanceId;
    }

    @Transactional
    public Page findOwnerHistory(int pageNo, int pageSize) {
        Page page = this.flowDao.findOwnerHistory(YgService.getCurrentYg().getId().toString(), pageNo, pageSize);
        List<Map> historyList = new ArrayList<Map>();
        for (Object[] obj : (List<Object[]>) page.getResult()) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("executionId", obj[0]);
            map.put("activityName", obj[1]);
            map.put("opera", obj[2]);
            map.put("time", obj[3]);
            historyList.add(map);
        }
        page.setResult(historyList);
        return page;
    }

    @Transactional
    public List<FlowTask> findAllTaskYgBack(String flowName, String taskName, String current, Long sbbmId, Long sbgsId) {
        if (StringUtils.isNotEmpty(current)) {
            if (sbbmId == 30 || sbbmId == 26 || sbbmId == 27 || sbbmId == 28
                    || sbbmId == 29 || sbbmId == 8131 || sbbmId == 8132
                    || sbbmId == 8133 || sbbmId == 8134) {
                return this.flowDao.findAllTaskYg(flowName, taskName, sbbmId);
            } else {
                return this.flowDao.findAllTaskYg(flowName, taskName, sbgsId);
            }
        } else
            return this.flowDao.findAllTaskYg(flowName, taskName);
    }


    @Transactional
    public List<FlowTask> findAllTaskYg(String flowName, String taskName, String current) {
        if (StringUtils.isNotEmpty(current)) {
            if (YgService.getCurrentYgGsId() == 30 || YgService.getCurrentYgGsId() == 26
                    || YgService.getCurrentYgGsId() == 27 || YgService.getCurrentYgGsId() == 28
                    || YgService.getCurrentYgGsId() == 29 || YgService.getCurrentYgGsId() == 8131
                    || YgService.getCurrentYgGsId() == 8132
                    || YgService.getCurrentYgGsId() == 8133
                    || YgService.getCurrentYgGsId() == 8134) {
                return this.flowDao.findAllTaskYg(flowName, taskName, YgService.getCurrentYgGsId());
            } else {
                return this.flowDao.findAllTaskYg(flowName, taskName, YgService.getCurrentYgZgGsId());
            }
        } else
            return this.flowDao.findAllTaskYg(flowName, taskName);
    }

    @Transactional
    public Boolean checkFlowStart(String flowName) {
        return this.flowDao.checkFlowStart(flowName);
    }

    @Autowired
    public void setFlowDao(FlowDao flowDao) {
        this.flowDao = flowDao;
    }

    @Autowired
    public void setProcessEngine(@Qualifier("processEngine") ProcessEngine processEngine) {
        this.processEngine = processEngine;
    }

    @Autowired
    public void setRepositoryService(@Qualifier("repositoryService") RepositoryService repositoryService) {
        this.repositoryService = repositoryService;
    }

    @Autowired
    public void setExecutionService(@Qualifier("executionService") ExecutionService executionService) {
        this.executionService = executionService;
    }

    @Autowired
    public void setTaskService(@Qualifier("taskService") TaskService taskService) {
        this.taskService = taskService;
    }
}
